package site.lisl.mynote.algorithm.thread;

import java.util.concurrent.locks.LockSupport;

/**
 * 买包子-》卖完了(等待）——》通知
 * @author lishuailei
 */
public class ThreadDemo {

    private static Object obj = null;

    public static void main(String[] args) throws InterruptedException {
//        testDeadLockBySuspendAndResumeThread();
        ThreadDemo threadDemo = new ThreadDemo();
        threadDemo.testDeadLockByParkAndUnParkThread();
    }

    private static void testSuspendAndResumeThread() throws InterruptedException {
        Thread consumerThread = new Thread(()->{
            while (obj == null){
                System.out.println("2.包子卖完了，等待中");
                Thread.currentThread().suspend();
            }
        });
        System.out.println("1.开始买包子");
        consumerThread.start();
        Thread.sleep(2000L);
        obj = new Object();
        //通知可以买包子了
        System.out.println("3.通知可以买包子了");
        consumerThread.resume();
        consumerThread.join();
    }

    private static void testDeadLockBySuspendAndResumeThread() throws InterruptedException {
        Thread consumerThread = new Thread(()->{
            while (obj == null){
                System.out.println("2.包子卖完了，等待中");
                synchronized (ThreadDemo.class) {
                    Thread.currentThread().suspend();
                }
            }
        });
        System.out.println("1.开始买包子");
        consumerThread.start();
        Thread.sleep(2000L);
        obj = new Object();
        //通知可以买包子了
        System.out.println("3.通知可以买包子了");
        synchronized (ThreadDemo.class) {
            consumerThread.resume();
        }
        consumerThread.join();
        System.out.println("4.买到包子了");
    }

    private void testWaitAndNotifyThread() throws InterruptedException {
        Thread consumerThread = new Thread(()->{
            while (obj == null){
                System.out.println("2.包子卖完了，等待中");
                synchronized (this){
                    try {
                        this.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        System.out.println("1.开始买包子");
        consumerThread.start();
        Thread.sleep(2000L);
        obj = new Object();
        //通知可以买包子了
        System.out.println("3.通知可以买包子了");
        synchronized (this) {
            this.notifyAll();
        }
        consumerThread.join();
        System.out.println("4.买到包子了");

    }

    private void testDeadLockByWaitAndNotifyThread() throws InterruptedException {
        Thread consumerThread = new Thread(()->{
            while (obj == null){
                System.out.println("2.包子卖完了，等待中");
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (this){
                    try {
                        this.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        System.out.println("1.开始买包子");
        consumerThread.start();
        Thread.sleep(2000L);
        obj = new Object();
        //通知可以买包子了
        System.out.println("3.通知可以买包子了");
        synchronized (this) {
            this.notifyAll();
        }
        consumerThread.join();
        System.out.println("4.买到包子了");

    }


    private void testParkAndUnParkThread() throws InterruptedException {
        Thread consumerThread = new Thread(()->{
            while (obj == null){
                System.out.println("2.包子卖完了，等待中");
                LockSupport.park();
            }
            System.out.println("4.买到包子了");
        });
        System.out.println("1.开始买包子");
        consumerThread.start();
        Thread.sleep(2000L);
        obj = new Object();
        //通知可以买包子了
        System.out.println("3.通知可以买包子了");
        LockSupport.unpark(consumerThread);

    }

    private void testDeadLockByParkAndUnParkThread() throws InterruptedException {
        Thread consumerThread = new Thread(()->{
            while (obj == null){
                System.out.println("2.包子卖完了，等待中");

                synchronized (this) {
                    LockSupport.park();
                }
            }
            System.out.println("4.买到包子了");
        });
        System.out.println("1.开始买包子");
        consumerThread.start();
        Thread.sleep(2000L);
        obj = new Object();
        //通知可以买包子了
        System.out.println("3.通知可以买包子了");
        synchronized (this) {
            LockSupport.unpark(consumerThread);
        }

    }

}
